<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:doc="livcos.org/ns/doc"
  xmlns:f="livcos.org/ns/xslt/function"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns="http://www.w3.org/1999/XSL/Format"
  exclude-result-prefixes="doc f xs">

  <xsl:import href="../base.xsl" />

  <xsl:import href="history.xsl" />
  <xsl:import href="outline.xsl" />
  <xsl:import href="inline.xsl" />
  <xsl:import href="image.xsl" />
  <xsl:import href="link.xsl" />
  <xsl:import href="list.xsl" />
  <xsl:import href="table.xsl" />
  <xsl:import href="markup.xsl" />
	<xsl:import href="embedding.xsl" />
	<xsl:import href="diagram.xsl" />
	
  <xsl:variable name="logo" select="''"/>

  <xsl:template match="/doc:document">
    <root>
    
      <layout-master-set>
        <simple-page-master master-name="page_A4" page-height="29.7cm" page-width="21cm"
          margin-top="1.5cm" margin-bottom="2cm" margin-left="2.5cm" margin-right="2.5cm">
          <region-body margin-top="1.6cm" margin-bottom="1cm" margin-left="0.5cm"
            margin-right="0.5cm" />
          <region-before extent="1.6cm" />
          <region-after extent="1cm" />
        </simple-page-master>
      </layout-master-set>

      <page-sequence master-reference="page_A4">
        <static-content flow-name="xsl-region-before">
          <xsl:call-template name="header"/>
        </static-content>
        <static-content flow-name="xsl-region-after">
          <xsl:call-template name="footer"/>
        </static-content>
        <flow flow-name="xsl-region-body" font-size="8pt" font-family="Verdana">
          <block margin-top="18pt" margin-bottom="16pt" font-size="16pt">
            <xsl:value-of select="doc:head/doc:context"/>
          </block>
          <block margin-top="12pt" margin-bottom="64pt" font-size="24pt"
            id="title">
            <xsl:value-of select="doc:head/doc:title" />
          </block>
          <xsl:apply-templates select="doc:history"/>
          <xsl:call-template name="toc"/>
          <block break-before="page"/>
          <xsl:apply-templates select="doc:body/*"/>
          <block id="last_page"></block>
        </flow>
      </page-sequence>
      
    </root>
  </xsl:template>

  <xsl:template name="header">
    <table table-layout="fixed" width="100%" border-bottom-style="solid" border-bottom-width="thin">
      <table-column column-width="25%"/>
      <table-column column-width="50%"/>
      <table-column column-width="25%"/>
      <table-body>
        <table-row>
          <table-cell padding="0pt">
            <block text-align="start">
              <xsl:if test="normalize-space($logo) != ''">
                <external-graphic src="{$logo}" content-height="25pt" vertical-align="middle"/>
              </xsl:if>
            </block>
          </table-cell>
          <table-cell padding="0pt">
            <block font-size="10pt" font-family="sans-serif" text-align="center" margin-top="15pt">
              <xsl:value-of select="doc:head/doc:context" />
              <xsl:text>: </xsl:text>
              <xsl:value-of select="doc:head/doc:title" />
            </block>
          </table-cell>
          <table-cell padding="0pt">
            <block font-size="10pt" font-family="sans-serif" text-align="end" margin-top="15pt">
              <page-number />/<page-number-citation ref-id="last_page" />
            </block>
          </table-cell>
        </table-row>
      </table-body>
    </table>
  </xsl:template>

  <xsl:template name="footer">
    <table table-layout="fixed" width="100%" border-top-style="solid" border-top-width="thin">
      <table-column column-width="20%"/>
      <table-column column-width="60%"/>
      <table-column column-width="20%"/>
      <table-body >
        <table-row>
          <table-cell padding="1pt">
            <block font-size="8pt" text-align="start">
              <xsl:value-of select="doc:history/doc:record[1]/@author"/>
            </block>
          </table-cell>
          <table-cell padding="1pt">
            <block font-size="8pt" text-align="center">
              <xsl:value-of select="f:object-id(.)" />
              <xsl:text>.pdf</xsl:text>
            </block>
          </table-cell>
          <table-cell padding="1pt">
            <block font-size="8pt" text-align="end">
              <xsl:variable name="date" as="xs:dateTime"
                select="if (doc:history/doc:record[last()]/@time) then xs:dateTime(doc:history/doc:record[last()]/@time) else current-dateTime()"/>
              <xsl:value-of select="format-date(xs:date($date), '[Y]-[M01]-[D01]')"/>
            </block>
          </table-cell>
        </table-row>
      </table-body>
    </table>
  </xsl:template>

  <xsl:template name="toc">
		<xsl:if test="doc:body/doc:chapter">
			<block break-before="page" color="{$title_color}" font-size="12pt" margin-bottom="6pt">
				<xsl:text>Inhaltsverzeichnis</xsl:text>
			</block>
			<xsl:apply-templates select="doc:body/doc:chapter" mode="toc"/>
		</xsl:if>
  </xsl:template>

  <xsl:template match="doc:chapter" mode="toc">
    <block text-align-last="justify" space-before="1pt" space-after="1pt" font-size="8pt"
      margin-left="6mm" margin-right="12mm">
      <basic-link internal-destination="{@id}">
        <xsl:number level="multiple" count="doc:chapter" format="1.1"/>
        <xsl:text> </xsl:text>
        <xsl:apply-templates select="doc:title/node()"/>
        <xsl:text> </xsl:text>
        <leader leader-length.minimum="12pt" leader-length.optimum="40pt"
          leader-length.maximum="100%" leader-pattern="dots"/>
        <xsl:text> </xsl:text>
        <page-number-citation ref-id="{@id}"/> 
      </basic-link>
    </block>
    <xsl:apply-templates select="doc:chapter" mode="toc"/>
  </xsl:template>

</xsl:stylesheet>
